import {
    createRouter,
    createWebHashHistory
} from "vue-router";

import store from '../store/index.js'

const routes = [{
        path: "/login",
        component: () => import("../view/Login/index.vue"),

        children: [],
        meta: {
            title: '登录页',
            hideMenu: true, //加入hideMenu属性，不展示在侧边栏
        },
        name: "Login",
    },
    {
        path: "/",
        component: () => import("../view/Home/index.vue"),
        meta: {
            keepalive: true,
            title: "主页",
        },
        name: 'Home',
        // hideMenu: true,//不展示在侧边栏
        children: [],
        redirect: '/index'
    },
]



const router = createRouter({
    history: createWebHashHistory(),
    routes
});

router.beforeEach(async (to, from, next) => { //路由守卫
    if (store.state.login.token) { //存在token
        if (to.path == '/login') { //存在token,如果想跳转到登录页,默认有token跳转进项目首页
            next({
                path: '/'
            })
        } else { //如果存在token,跳转进项目页面,则判断当前后端返回的路由有无长度或者有无即将跳转路由的name
            if (store.getters['login/getRoutes'].length || to.name != null) {
                next() //有的话直接跳转
            } else { //不满足条件的话,重新请求后端动态路由数据
                await addRoutes(); //addRoutes()必须加入await!!!!等待逻辑执行完毕获取路由
                // 如果 addRoute 未完成，路由守卫会一层一层的执行执行.可能导致卡死!!!，直到 addRoute 完成，找到对应的路由
                next({
                    ...to,
                    replace: true
                })
            }
        }
    } else {
        if (to.path == '/login') {
            next()
        } else {
            next('/login')
        }
    }
})
let errorRoute = {
    path: "/404",
    component: () => import("../view/ErrorPage/index.vue"),
    meta: {
        keepalive: true,
        title: "404页面",
        hideMenu: true, //不展示在侧边栏
    },
    name: '404页面',

    children: [],
}

// let baseRoutes = [...routes] //初始的路由

let asyncRoutes = [] //定义数组接收后端返回的路由

const routeAllPathToCompMap =
    import.meta.glob(`../view/**/*.vue`);
// console.log(routeAllPathToCompMap);//routeAllPathToCompMap是一个数组
//**为通配符vite不支持require导入方式,故用import.meta.glob(vite动态导入)
/*import.meta.glob
 * 该方法匹配到的文件默认是懒加载，通过动态导入实现，构建时会分离独立的 chunk，是异步导入，返回的是 Promise
 * /*import.meta.globEager
 * 该方法是直接导入所有模块，并且是同步导入，返回结果直接通过 for...in循环就可以操作
 */
async function addRoutes() {
    await store.dispatch('login/getNewRoutes').then((res) => { //获取后端返回的动态路由
        if (res.data && res.data.length) {
            // let homeRouteChildren = [];
            asyncRoutes = res.data;
            /*
             * 1。拿到处理完毕home的children，最终替换掉原来的children，给菜单渲染提供支持
             * 2.通过递归,调用router.addRoute,把每一项route插到对应的父route下
             */
            //服务端配置了路由，但前端还没添加对应文件的路由列表，内容是路由的component值(服务端的)
            // const unForList = ['']
            const homeChildren = routes[1].children;
            const dfs = (parentRouteName = 'Home', asyncRoutes = [], originRouteChildren = []) => {
                if (!Array.isArray(asyncRoutes)) return [];
                asyncRoutes.forEach(route => {
                    // if (unForList.includes(route.component)) return;
                    /**后端返回来的路由component是字符串,如component: "view/Index/index.vue",
                     * 前端需要把component: "view/Index/index.vue" 转化为组件对象
                     * component:() => import("/src/view/Index/index.vue")
                     **/
                    route.component = routeAllPathToCompMap[`../${route.component}`];
                    // route.component = () => import(`../${route.component}`);
                    const routeChildren = route.children;

                    router.addRoute(parentRouteName, route);

                    route.children = dfs(route.name, routeChildren)

                    originRouteChildren.push(route)
                })
                return originRouteChildren
            }
            // homeRouteChildren = dfs(asyncRoutes)
            dfs('Home', asyncRoutes, homeChildren)
        }
    });
    console.log(asyncRoutes);
}
// router.addRoute(homeRoute)
// asyncRoutes.forEach(item => {
//     baseRoutes[1].children.push(item)
//     router.addRoute({
//         path: `${item.path}`,
//         name: item.name,
//         meta: {
//             title: item.title
//         },
//         component: () => import(`../${item.component}`)
//     })
// })
// console.log(router)


export {
    router,
    addRoutes
};
//在main.js中进行引入，并进行挂载注册到全局上